package com.css.fxfzaqbz.modules.maps.service.impl;

import cn.hutool.core.util.PageUtil;
import com.css.fxfzaqbz.base.response.RestResponse;
import com.css.fxfzaqbz.constants.YNEnum;
import com.css.fxfzaqbz.modules.base.service.HypergraphService;
import com.css.fxfzaqbz.modules.callJcData.entity.PageResult;
import com.css.fxfzaqbz.modules.maps.vo.MapDelVO;
import com.css.fxfzaqbz.modules.maps.vo.MapQueryVO;
import com.css.fxfzaqbz.modules.maps.vo.MapUploadVO;
import com.css.fxfzaqbz.modules.maps.vo.MapViewVO;
import com.css.fxfzaqbz.modules.maps.entity.MapType;
import com.css.fxfzaqbz.modules.maps.entity.ProfessionMap;
import com.css.fxfzaqbz.modules.maps.repository.MapManagerRepository;
import com.css.fxfzaqbz.modules.maps.service.MapManagerService;
import com.css.fxfzaqbz.util.CreateGson;
import com.css.fxfzaqbz.util.PlatformObjectUtils;
import com.css.fxfzaqbz.util.ResponseCreator;
import com.css.fxfzaqbz.util.UUIDGenerator;
import com.css.fxfzaqbz.web.PlatformSessionContext;
import com.css.fxfzaqbz.zcpt.sys.entity.SUser;
import com.css.fxfzaqbz.zcpt.sys.service.SUserService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author Dbj
 * @date 2023年02月07日  10:35
 */
@Service
public class MapManagerServiceImpl implements MapManagerService {

    @Resource
    private SUserService sUserService;
    @Resource
    private MapManagerRepository mapManagerRepository;
    @Resource
    private HypergraphService hypergraphService;

    @Override
    public PageResult<ProfessionMap> page(MapQueryVO param) {
        int tPNum = param.getPage();
        param.setPage(param.getRows() * (Math.max(param.getPage(), 1) - 1));
        List<ProfessionMap> list = mapManagerRepository.page(param);
        List<MapType> mapType = this.getMapType();
        Map<String, String> map = mapType.stream().collect(Collectors.toMap(MapType::getType,MapType::getTypeName));
        list.forEach(entity->{
            entity.setType(map.get(entity.getType()));
        });
        Integer total = mapManagerRepository.countForPage(param);
        return new PageResult<>(tPNum, param.getRows(), PageUtil.totalPage(total, param.getRows()), total, list);
    }

    @Override
    public List<MapType> getMapType() {
        return mapManagerRepository.getMapType();
    }


    @Override
    @Transactional(rollbackFor = Exception.class)
    public void del(MapDelVO vo) {
        if(PlatformObjectUtils.isNotEmpty(vo.getIds())){
            List<String> ids = vo.getIds();
            mapManagerRepository.del(ids);
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public RestResponse uploadSave(MapUploadVO vo) throws Exception{
        //查看是否存在相同类型相同名称的附件信息
        Integer exist = mapManagerRepository.getProfessionMapMsg(vo.getType(),vo.getName());
        if(exist>0){
            return RestResponse.fail("上传失败,存在相同类型相同名称的图件模板");
        }
        //上传模板文件到超图模板目录
        String templateName = null;
        RestResponse res = hypergraphService.uploadTemplateFile(vo.getAttachId());
        if(res.getCode()==200){
            templateName = (String) res.getData();
        }else{
            return res;
        }
        if (PlatformObjectUtils.isEmpty(templateName)){
            return RestResponse.fail("上传模板文件到超图模板目录失败");
        }
        vo.setTemplateName(templateName);//超图Web打印模板名称
        vo.setId(UUIDGenerator.getUUID());
        vo.setUploadTime(new Date());
        vo.setUploadUserId(PlatformSessionContext.getUserID());
        String userJson = sUserService.getSUser(PlatformSessionContext.getUserID());
        SUser sUser = CreateGson.createGson().fromJson(userJson, SUser.class);
        if (sUser!=null){
            vo.setUploadUserName(sUser.getUserName());
        }else{
            vo.setUploadUserName("");
        }
        vo.setDelFlag(YNEnum.N.toString());
        mapManagerRepository.uploadSave(vo);
        return RestResponse.succeed("保存成功");
    }

    @Override
    public MapViewVO view(String id){
        return mapManagerRepository.view(id);
    }

    @Override
    public void update(MapViewVO vo) {
        vo.setUpdateTime(new Date()).setUpdateUserId(PlatformSessionContext.getUserID());
        mapManagerRepository.update(vo);
    }

    @Override
    public List<MapViewVO> getTypeNameByType(String type) {
        return mapManagerRepository.getTypeNameByType(type);
    }
}
